package com.vision.sales.sv.serv.rpc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;
import com.vision.core.cm.data.Domain;
import com.vision.core.cm.exception.DataNotFoundException;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.sv.db.mgr.IDataManager;
import com.vision.core.sv.db.mgr.ISimpleCriteria;
import com.vision.core.sv.db.mgr.SimpleCriteria;
import com.vision.core.sv.serv.rpc.AbstractServiceImpl;
import com.vision.sales.cm.db.data.Check;
import com.vision.sales.cm.serv.rpc.CheckService;

import static com.vision.sales.cm.data.SalesPermissions.*;

/**
 * 
 * @author Mark
 *
 */
@Service("check")
public class CheckServiceImpl extends AbstractServiceImpl<Check> implements CheckService {

	private static final long serialVersionUID = 1L;
	
	public CheckServiceImpl() {
		initGilead();
	}
	
	@Autowired
	@Qualifier("checkManager")
	@Override
	public void setDataManager(IDataManager<Check> dataManager) {
		super.setDataManager(dataManager);
	}
	
	@Override
	protected Domain getDomain() {
		return Domain.CHECK;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PagingLoadResult<Check> getList(PagingLoadConfig config) throws RpcException {
		checkPermissions(ViewCheck);
		
		ISimpleCriteria crit = SimpleCriteria.forClass(Check.class);
		
		List<Check> checks = getTemplate().findByCriteria(crit.getDetached(), config.getOffset(), config.getLimit());
		
		crit.count();
		Long count = (Long) getTemplate().findByCriteria(crit.getDetached()).get(0);
		
		PagingLoadResult<Check> result = new PagingLoadResultBean<Check>(checks, count.intValue(), config.getOffset());
		
		return result;
	}
	
	@Override
	public List<Check> getAll() throws RpcException {
		checkPermissions(ViewCheck);
		
		return super.getAll();
	}

	@Override
	public Check create(Check check) throws RpcException {
		checkPermissions(CreateCheck);
		
		return super.create(check);
	}

	@Override
	public Check update(Check check) throws RpcException {
		checkPermissions(UpdateCheck);
		
		Check existing = getById(check.getId());
		if (existing == null)
			throw new DataNotFoundException("The check " + check.getId() + " does not exist.");
		
		check.setBank(existing.getBank());
		check.setCheckNo(existing.getCheckNo());
		check.setCheckDate(existing.getCheckDate());
		check.setAmount(existing.getAmount());
		
		return super.update(check);
	}
	
	@Override
	public Check delete(Check object) throws RpcException {
//		checkPermissions(DeleteCheck);
//		
//		return super.delete(object);
		throw new RpcException("Checks cannot be deleted.");
	}

}
